package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.Random;

import client.EosApiClientFactory;
import client.EosApiRestClient;
import com.azazar.bitcoin.jsonrpcclient.BitcoinException;
import com.azazar.bitcoin.jsonrpcclient.BitcoinJSONRPCClient;
import io.renren.common.eth.web3j.EthUtil;
import io.renren.common.utils.UUIDUtils;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.WalletAccountsEntity;
import io.renren.modules.sys.service.WalletAccountsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 钱包账户
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-06-26 21:10:59
 */
@RestController
@RequestMapping("sys/walletaccounts")
public class WalletAccountsController {
    @Autowired
    private WalletAccountsService walletAccountsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:walletaccounts:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = walletAccountsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:walletaccounts:info")
    public R info(@PathVariable("id") Long id){
        WalletAccountsEntity walletAccounts = walletAccountsService.selectById(id);

        return R.ok().put("walletAccounts", walletAccounts);
    }

    /**
     * 创建区块链钱包账户
     */
    @RequestMapping("/createAccount")
    @RequiresPermissions("sys:walletaccounts:createAccount")
    public R createAccount(String walletType){
        //  判断需要创建账号类型 创建账号
        WalletAccountsEntity walletAccounts = new WalletAccountsEntity();
        String walletAccount = "";
        if("eth".equalsIgnoreCase(walletType)){
            // 1、eth
            try {
                walletAccount = EthUtil.createAccount(UUIDUtils.getUUID());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if("eos".equalsIgnoreCase(walletType)){
            //2、eos
            String accountRandomString = getRandomString(12);
            EosApiRestClient eosApiRestClient = EosApiClientFactory.newInstance("http://127.0.0.1:8888").newRestClient();
            walletAccount = eosApiRestClient.createWallet(accountRandomString);
        }else {
            //3、bitcoin
            try {
                walletAccount = new BitcoinJSONRPCClient().getAccountAddress(UUIDUtils.getUUID());
            } catch (BitcoinException e) {
                e.printStackTrace();
            }
        }
        walletAccounts.setWalletAccount(walletAccount);
        walletAccountsService.insert(walletAccounts);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:walletaccounts:save")
    public R save(@RequestBody WalletAccountsEntity walletAccounts){
        walletAccountsService.insert(walletAccounts);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:walletaccounts:update")
    public R update(@RequestBody WalletAccountsEntity walletAccounts){
        ValidatorUtils.validateEntity(walletAccounts);
        walletAccountsService.updateAllColumnById(walletAccounts);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:walletaccounts:delete")
    public R delete(@RequestBody Long[] ids){
        walletAccountsService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    public static String getRandomString(int length) { // length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz12345";
        return getRandomString(length, base);
    }

    public static String getRandomString(int length,String charSource) { // length表示生成字符串的长度
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(charSource.length());
            sb.append(charSource.charAt(number));
        }
        return sb.toString();
    }

}
